﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XTYAPI.文件
{
    public class CfgDicJson : Dictionary<string, object>
    {
        public CfgDicJson Load(string path)
        {         
            CfgDicJson keyValues=new CfgDicJson();
            Com.LoadFile(path, ref keyValues);          
            return keyValues;
        }
        public bool GetValueBool(string key)
        {
            if (ContainsKey(key))
            {
                string value = this[key].ToString();
                bool result;
                if (bool.TryParse(value, out result))
                {
                    return result;
                }
            }
            return false;
        }
        public int GetValueInt(string key)
        {
            if (ContainsKey(key))
            {
                string value = this[key].ToString();
                int result;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
            }
            return 0;
        }
        public uint GetValueUInt(string key)
        {
            if (ContainsKey(key))
            {
                string value = this[key].ToString();
                uint result;
                if (uint.TryParse(value, out result))
                {
                    return result;
                }
            }
            return 0;
        }
        public double GetValueDouble(string key)
        {
            if (ContainsKey(key))
            {
                string value = this[key].ToString();
                double result;
                if (double.TryParse(value, out result))
                {
                    return result;
                }
            }
            return 0;
        }
        public float GetValueFloat(string key)
        {
            if (ContainsKey(key))
            {
                string value = this[key].ToString();
                float result;
                if (float.TryParse(value, out result))
                {
                    return result;
                }
            }
            return 0;
        }
        public string GetValueString(string key)
        {
            if (ContainsKey(key))
            {
                return Convert.ToString(this[key]);
            }
            return "";
        }
        public object GetValueObject(string key)
        {
            if (ContainsKey(key))
            {
                return this[key];
            }
            return null;
        }
        public void SetValue(string key, object value)
        {
            if (ContainsKey(key))
            {
                this[key] = value;
            }
            else
            {
                Add(key, value);
            }
        }
        public void SetValue(string key, bool value)
        {
            SetValue(key, value);
        }

        public void SetValue(string key, int value)
        {
            SetValue(key, value);
        }
    }
}
